unit U_fm_rel_compra;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.Mask,
  {RpBase, RpSystem, RpRave, RpDefine, RpCon, RpConDS,} Vcl.ComCtrls;

type
  Tfm_rel_compra = class(TForm)
    GroupBox3: TGroupBox;
    EditNfe: TEdit;
    GroupBox2: TGroupBox;
    SpeedButton1: TSpeedButton;
    EditFor: TEdit;
    GroupBox1: TGroupBox;
    Label2: TLabel;
    Label1: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Date_ini: TDateTimePicker;
    Date_fim: TDateTimePicker;
    procedure BitBtn1Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fm_rel_compra: Tfm_rel_compra;

implementation

{$R *.dfm}

uses U_dm_principal, U_fm_pesq_for;

procedure Tfm_rel_compra.BitBtn1Click(Sender: TObject);
var
  MySql: String;
  MyWhere: String;
  MyCount: Integer;
const
  MyFieldNF    = 'n_nfe';
  MyFieldFOR  = 'fornecedor';
  MyFieldDtI = 'dt_emissao';

begin

  MySql :=  'select dt_emissao,'
            +' fornecedor,'
            +' cfop, '
            +' tipo_pag,'
            +' frete, '
            +' id,'
            +' n_nfe'
            +' from entrada ' ;
  MyWhere := EmptyStr;
  MyCount := 0;

  //-
  try
    dm_principal.sq_entrada.Close;
    dm_principal.cds_entrada.Close;

    if EditNfe.Text <> EmptyStr then
     begin
       MyWhere := 'WHERE ' + MyFieldNF + '=' + QuotedStr(EditNfe.Text);
       MyCount := 1;
     end;

      if Date_ini.Checked then
      MyWhere:=(' AND ' + MyFieldDtI + ' BETWEEN ' + QuotedStr((FormatDateTime('YYYY/MM/DD',Date_ini.Date)))+' AND '+QuotedStr((FormatDateTime('YYYY/MM/DD',Date_fim.Date))));

    //-
    if (EditFor.Text <> EmptyStr) and (MyCount = 0) then
      begin
         MyWhere := 'WHERE ' + MyFieldFOR+ '=' + QuotedStr(EditFor.Text);
         MyCount := 1;

      end


    else



     finally
    dm_principal.sq_entrada.SQL.Text := MySql + MyWhere;
    dm_principal.sq_entrada.Open;
    dm_principal.cds_entrada.Open;

        //RvProject_compra.Open;
        //RvProject_compra.SelectReport('project_compra',false);
        //RvProject_compra.Execute;
        //RvProject_compra.Close;
end;

end;
procedure Tfm_rel_compra.FormShow(Sender: TObject);
begin
Date_ini.Date:=Date;
Date_fim.Date:=Date;
end;

procedure Tfm_rel_compra.SpeedButton1Click(Sender: TObject);
begin
fm_pesq_for:=Tfm_pesq_for.Create(Application);
  fm_pesq_for.ShowModal;
 if fm_pesq_for.ModalResult=mrok then
 begin
   EditFor.Text:=dm_principal.cds_fornecedorNOME.Text;
 end;
end;

end.
